package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.merchant.result.MerchantResult;
import com.coffee.admin.api.supplier.rep.SupplierDetailRep;
import com.coffee.admin.api.supplier.rep.SupplierListRep;
import com.coffee.admin.api.supplier.req.*;
import com.coffee.admin.api.supplier.result.SupplierResult;
import com.coffee.admin.api.supplier.service.SupplierService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.PasswordUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/platform")
public class PlatformSupplierController extends BaseController {

    @DubboReference
    private SupplierService supplierService;

    @OperLog(operModul = "平台-供应商管理", operDesc = "查询供应商管理列表")
    @PlatformLoginCheck(alias = "SupplierManageListAlias")
    @PostMapping(value = "/supplierList")
    public Result<Page<SupplierListRep>> supplierList(@RequestBody SupplierListReq req, HttpServletRequest request, HttpServletResponse response) {
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        Page<SupplierListRep> supplierListRepPage = supplierService.getPager(req, pagerInfo);
        return Result.success(supplierListRepPage);
    }
 

    @OperLog(operModul = "平台-供应商管理-删除供应商", operDesc = "供应商管理-删除供应商")
    @PlatformLoginCheck(alias = "SupplierOperationAlias")
    @PostMapping(value = "/supplier/delete")
    public Result<String> supplierDelete(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) {
        Integer id = json.getInteger("id");
        supplierService.delete(id);
        return Result.success("SUCCESS");
    }

    @OperLog(operModul = "平台-供应商管理-变更供应商状态", operDesc = "供应商管理-变更供应商状态")
    @PlatformLoginCheck(alias = "SupplierOperationAlias")
    @PostMapping(value = "/supplier/updateIsEnable")
    public Result<String> updateIsEnable(@RequestBody SupplierUpdateIsEnableReq req, HttpServletRequest request, HttpServletResponse response) {
        try {
            supplierService.updateIsEnable(req);
            return Result.success("SUCCESS");
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
    }


    @OperLog(operModul = "平台-供应商管理-新增供应商", operDesc = "供应商管理-新增供应商")
    @PlatformLoginCheck(alias = "SupplierOperationAlias")
    @PostMapping(value = "/supplier/add")
    public Result<String> supplierAdd(@RequestBody SupplierAddReq req, HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        SupplierResult result = new SupplierResult();
        BeanUtilExt.copyProperties(result, req);
        result.setPassword(PasswordUtil.getPass(req.getPassword()));
        result.setIsEnable("Y");
        result.setIsAdmin("Y");
        supplierService.add(result);
        return Result.success("SUCCESS");
    }


    @OperLog(operModul = "平台-供应商管理-供应商详情", operDesc = "供应商管理-供应商详情")
    @PlatformLoginCheck(alias = "SupplierManageListAlias")
    @PostMapping(value = "/supplier/detail")
    public Result<SupplierDetailRep> supplierDetail(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) {
        Integer id = json.getInteger("id");
        SupplierDetailRep supplierDetailRep = supplierService.platformGetDetail(id);
        return Result.success(supplierDetailRep);
    }

    @OperLog(operModul = "平台-供应商管理-变更手机号", operDesc = "供应商管理-变更手机号")
    @PlatformLoginCheck(alias = "SupplierOperationAlias")
    @PostMapping(value = "/supplier/updateMobile")
    public Result<String> supplierUpdateMobile(@RequestBody SupplierUpdateMobileReq req, HttpServletRequest request, HttpServletResponse response) {
        try {
            supplierService.updateMobile(req);
            return Result.success("SUCCESS");
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
    }


    @OperLog(operModul = "平台-供应商管理-变更密码", operDesc = "供应商管理-变更密码")
    @PlatformLoginCheck(alias = "SupplierOperationAlias")
    @PostMapping(value = "/supplier/updatePassword")
    public Result<String> supplierUpdatePassword(@RequestBody SupplierUpdatePasswordReq req, HttpServletRequest request, HttpServletResponse response) {
        try {
            supplierService.updatePassword(req);
            return Result.success("SUCCESS");
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
    }

    @OperLog(operModul = "平台-供应商管理-变更购买权限", operDesc = "供应商管理-变更购买权限")
    @PlatformLoginCheck(alias = "SupplierOperationAlias")
    @PostMapping(value = "/supplier/updateBuyPermission")
    public Result<String> updateBuyPermission(@RequestBody SupplierUpdateBuyPermissionReq req, HttpServletRequest request, HttpServletResponse response) {
        try {
            supplierService.updateBuyPermission(req);
            return Result.success("SUCCESS");
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
    }

    @OperLog(operModul = "平台-商户管理-变更商户状态", operDesc = "商户管理-变更商户状态")
    @SupplierLoginCheck(alias = "MerchantManageOperationAlias")
    @PostMapping(value = "/supplier/updateRemark")
    public Result<String> updateRemark(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) {
        try {
            String remark = jsonObject.getString("remark");
            Integer id = jsonObject.getInteger("id");
            if (StringUtils.isEmpty(remark)){
                throw BusinessException.build(ExceptionEnum.ERROR_1345);
            }
            SupplierAccountUpdateReq merchantResult=new SupplierAccountUpdateReq();
            merchantResult.setId(id);
            merchantResult.setRemark(remark);
            supplierService.updateRemark(merchantResult);
            return Result.success("SUCCESS");
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
    }

}
